package com.klb.service.catalog.impl;

import com.klb.dao.impl.UserDaoImpl;
import com.klb.model.BbsUser;
import com.klb.service.catalog.LoginServiceCatalog;
import com.klb.tools.CryptoUtils;
import com.klb.tools.Sessions;
import com.klb.tools.Tools;

/**
 * Created by fujia on 2016/5/27.
 */
public class LoginServiceCatalogImpl implements LoginServiceCatalog {

    private UserDaoImpl userDao;

    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }


    @Override
    public boolean loginIsSuccess(String telephone, String password) {

        /**
         * telephne不空且password不空
         * 创造bbsUser
         * bbsUser不空
         * 密码验证
         * 存放信息到session，返回结果
        * */
        if(!Tools.objectIsNullOrNot(telephone) && !Tools.objectIsNullOrNot(password)){
            BbsUser bbsUser = this.userDao.getUserByTelephone(telephone);
            if (!Tools.objectIsNullOrNot(bbsUser)) {
                if (CryptoUtils.verify(bbsUser.getPassword(), password, bbsUser.getSalt())){
                    Sessions.setSession("username", bbsUser.getName());
                    Sessions.setSession("userId", bbsUser.getUserId());
                    return true;
                }
            }
        }

        return false;
    }


}
